package com.danny.Controller;

import com.danny.mapper.*;
import com.danny.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class CheckOutController {
    @Autowired
    private CheckOutMapper checkOutMapper;
    @Autowired
    private CheckInMapper checkInMapper;
    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private VipMapper vipMapper;
    @Autowired
    private SumInfoMapper sumInfoMapper;
    @Autowired
    private RoomReserveMapper roomReserveMapper;



    //修改房间和预定信息
    @GetMapping("/updateRoomAndReserveByCheckInNo")
    public String updateRoomAndReserveByCheckInNo(int checkInNo){
        List<CheckIn>clist=checkInMapper.queryAllCheckIn();
        CheckIn checkIn=null;
        for (CheckIn c:
             clist) {
            if(c.getCheckinNo()==checkInNo){
                checkIn=c;
                break;
            }
        }

        //checkInMapper.deleteCheckInById(checkIn.getCheckinNo());

        if(checkIn==null)return "error";
        Room room=new Room();
        room.setRoomNo(checkIn.getRoomNo());
        room.setState("空房");
        if(roomMapper.updateRoom(room)==0)
            return "error";
        List<RoomReserve>rlist=roomReserveMapper.queryAllRoomReserve();
        RoomReserve roomReserve=null;
        for (RoomReserve r:
             rlist) {
            if(r.getRoomNo()==checkIn.getRoomNo()){
                roomReserve=r;
                break;
            }
        }

        checkIn.setLeftTime(new Timestamp(new Date().getTime()));
        checkIn.setCheckinNo(checkInNo);
        checkInMapper.updateCheckIn(checkIn);

        if(roomReserve==null)return "updated";
        if(roomReserveMapper.deleteRoomReserveByReserveNo(roomReserve.getReserveNo())==0)
            return "error";

        return "updated";
    }

    //可根据入住编号、房间编号、支付方式、操作员工编号查询退房信息
    @GetMapping("/queryCheckOutHasInput")
    public List<CheckOut> queryCheckOutHasInput(CheckOut checkOut){
        return checkOutMapper.queryCheckOutHasInput(checkOut);
    }

    @GetMapping("/queryCheckOutLimit")
    public List<CheckOut> queryCheckOutLimit(int page,int pageSize){
        Map<String,Object>map=new HashMap<>();
        map.put("page",page);
        map.put("pageSize",pageSize);
        return checkOutMapper.queryCheckOutLimit(map);
    }

    //分页查询，可根据入住编号、房间编号、支付方式、操作员工编号查询退房信息
    @GetMapping("/queryCheckOutLimitHasInput")
    public List<CheckOut> queryCheckOutLimitHasInput(CheckOut checkOut, int page, int pageSize){
        Map<String,Object>map=new HashMap<>();
        map.put("page",page);
        map.put("pageSize",pageSize);
        map.put("operatorNo",checkOut.getOperatorNo());
        map.put("checkInNo",checkOut.getCheckInNo());
        map.put("roomNo",checkOut.getRoomNo());
        map.put("payWay",checkOut.getPayWay());
        return checkOutMapper.queryCheckOutLimitHasInput(map);
    }

    @GetMapping("/queryCheckOutBillByCheckInNo")
    public CheckOutBill queryCheckOutBillByCheckInNo(int checkInNo){
        CheckOutBill bill=new CheckOutBill();
        CheckOut checkOut=generateCheckOutByCheckInNo(checkInNo);
        if(checkOut==null)return bill;
        bill.setCheckInNo(checkInNo);
        bill.setRoomNo(checkOut.getRoomNo());
        bill.setDeposit(checkOut.getDeposit());
        bill.setDays(getDays(checkInNo));
        bill.setTotalPrice(checkOut.getShouldPrice());
        if(bill.getDays()==0){
            bill.setDays(1);
        }
        int itemPrice=0;
        SumInfo sumInfo=getSumInfoByCheckInNo(checkInNo);
        if(sumInfo!=null){
            itemPrice=sumInfo.getSum();
        }
        bill.setItemSum(itemPrice);
        bill.setRoomPriceSum(checkOut.getShouldPrice()-itemPrice);
        bill.setRoomPrice(bill.getRoomPriceSum()/bill.getDays());
        return bill;
    }

    //根据入住单号、付款方式、实际付款金额、退回押金额度、操作员工编号添加退房信息
    @GetMapping("/addCheckOutByCheckInNoAndPay")
    public String addCheckOutByCheckInNoAndPay
    (int checkInNo, String payWay, int actualPrice, int refundDeposit, int operatorNo){
        CheckOut checkOut=generateCheckOutByCheckInNo(checkInNo);
        if(checkOut==null)return "error";
            checkOut.setPayWay(payWay);
            checkOut.setActualPrice(actualPrice);
        if(actualPrice>=checkOut.getShouldPrice())
            checkOut.setReturnChange(actualPrice-checkOut.getShouldPrice());
        else
            return "payment not enough";
        checkOut.setOperatorNo(operatorNo);
        checkOut.setRefundDeposit(refundDeposit);
        return addCheckOut(checkOut);
    }

    @GetMapping("/addCheckOut")
    public String addCheckOut(CheckOut checkOut){
        List<CheckOut>list=queryAllCheckOut();
        for (CheckOut c:
             list) {
            if(c.getCheckInNo()==checkOut.getCheckInNo())
                return "repetitive";
        }
        if(checkOutMapper.addCheckOut(checkOut)==0){
            return "error";
        }
        return "inserted";
    }

    @GetMapping("/queryAllCheckOut")
    public List<CheckOut> queryAllCheckOut(){
        return checkOutMapper.queryAllCheckOut();
    }

    @GetMapping("/checkOutCount")
    public int checkOutCount(){
        return checkOutMapper.checkOutCount();
    }

    @GetMapping("/checkOutCountHasInput")
    public int checkOutCountHasInput(CheckOut checkOut){
        return checkOutMapper.checkOutCountHasInput(checkOut);
    }

    @GetMapping("/updateCheckOut")
    public String updateCheckOut(CheckOut checkOut){
        if(checkOutMapper.updateCheckOut(checkOut)==0){
            return "error";
        }
        return "updated";
    }

    @GetMapping("/deleteCheckOutByCheckInNo")
    public String deleteCheckOutByCheckInNo(int checkInNo){
        if(checkOutMapper.deleteCheckOutByCheckInNo(checkInNo)==0){
            return "error";
        }
        return "deleted";
    }

    private CheckOut generateCheckOutByCheckInNo(int checkInNo){
        CheckOut checkOut=new CheckOut();
        checkOut.setCheckInNo(checkInNo);
        int shouldPrice=0;

        CheckIn checkIn=getCheckInByCheckInNo(checkInNo);
        if(checkIn==null) return null;
        checkOut.setRoomNo(checkIn.getRoomNo());

        checkOut.setDays(getDays(checkInNo));
        if(getDays(checkInNo)<=0){
            checkOut.setDays(1);
        }

        checkOut.setPrice(checkIn.getPrice());
        checkOut.setNewPrice(checkIn.getNewPrice());
        checkOut.setDeposit(checkIn.getDeposit());
        shouldPrice=checkIn.getNewPrice()*checkOut.getDays();

        Vip vip=getVipByTel(checkIn.getTel());
        if(vip!=null){
            Room room=roomMapper.queryRoomByRoomNo(checkIn.getRoomNo());
            shouldPrice=room.getVipPrice()*checkOut.getDays();
        }

        SumInfo sumInfo=getSumInfoByCheckInNo(checkInNo);
        if(sumInfo!=null){
            shouldPrice+=sumInfo.getSum();
        }

        checkOut.setShouldPrice(shouldPrice);
        return checkOut;
    }

    private CheckIn getCheckInByCheckInNo(int checkInNo){
        List<CheckIn>checkInList=checkInMapper.queryAllCheckIn();
        for (CheckIn c:
                checkInList) {
            if(c.getCheckinNo()==checkInNo){
                return c;
            }
        }
        return null;
    }

    public int getDays(int checkInNo) {
        CheckIn checkIn=getCheckInByCheckInNo(checkInNo);
        if(checkIn==null) return 0;
        String rTime=checkIn.getReachTime().toString();

        //
        String s = new Timestamp(new Date().getTime()).toString();
        String substring = s.substring(0, s.indexOf('.'));
        String lTime=substring;
        //System.out.println(lTime);
        String r=rTime.substring(0,10)+" 00:00:00.0";
        String l=lTime.substring(0,10)+" 00:00:00.0";
        Timestamp rt=Timestamp.valueOf(r);
        Timestamp lt=Timestamp.valueOf(l);
        int days=(int)(lt.getTime()-rt.getTime());
        days/=(1000*60*60*24);
        if(Integer.valueOf(lTime.substring(11,13))>=18)
            days+=1;
        return days;
    }

    private SumInfo getSumInfoByCheckInNo(int checkInNo){
        List<SumInfo>list=sumInfoMapper.queryAllSumInfo();
        for (SumInfo s:
             list) {
            if(s.getCheckinNo()==checkInNo){
                return s;
            }
        }
        return null;
    }

    private Vip getVipByTel(String tel){
        List<Vip> vipList=vipMapper.queryAllVip();
        for (Vip v:
             vipList) {
            if(v.getTel().equals(tel)){
                return v;
            }
        }
        return null;
    }


}
